package wiiusej.apptest.statistics.performance;

import java.sql.Timestamp;
import java.util.Vector;

import com.sun.org.apache.bcel.internal.generic.NEW;

import wiiusej.Wiimote;


public abstract class PerformanceUnit {

	private long wiimoteID;
	private long packetID;
	protected Vector<Track> tracks = new Vector<Track>();
	protected boolean complete = false;
	private long totalDelay; //milliseconds
	private long totalDelayNanos; //milliseconds
	
	
	protected int MAX_TRACKS;
	
	
	private final int NANOS_FACTOR = 1000000;
	
	//private Timestamp timeStamp; 
	
	
	public PerformanceUnit(long wiimote, long packetID){
		this.wiimoteID = wiimote;
		this.packetID = packetID;		
	}


	public long getWiimote() {
		return wiimoteID;
	}


	public void setWiimote(long wiimoteID) {
		this.wiimoteID = wiimoteID;
	}

	public long getPacketID() {
		return packetID;
	}


	public void setPacketID(long packetID) {
		this.packetID = packetID;
	}


	public long getWiimoteID() {
		return wiimoteID;
	}


	public void setWiimoteID(long wiimoteID) {
		this.wiimoteID = wiimoteID;
	}


	public Vector<Track> getTracks() {
		return tracks;
	}


	public void setTracks(Vector<Track> tracks) {
		this.tracks = tracks;
	}


	public boolean isCompleted(){
		if(complete){
			return true;			
		}		
		return false;		
	}
	
	
	public boolean addTrack(Track track) {
		
		if(!complete){			
			tracks.add(track);			
			if(tracks.size()==MAX_TRACKS){
				this.complete = true;
				computeTotalDelay();
			}			
			return true;
		}else{
			//Exception
			return false;			
		}	
	}
	
	protected void computeTotalDelay(){
		
		totalDelayNanos = tracks.lastElement().getTimeIN().getNanos() 
			- tracks.firstElement().getTimeIN().getNanos();
		
		if(tracks.lastElement().getTimeIN().getSeconds() > 
			tracks.firstElement().getTimeIN().getSeconds()){			
			
			totalDelayNanos = totalDelayNanos + 1000;
		}
		
		totalDelay = tracks.lastElement().getTimeIN().getTime() 
		- tracks.firstElement().getTimeIN().getTime();
	}


	public long getTotalDelay() {
		return totalDelay;
	}


	public long getTotalDelayNanos() {
		return totalDelayNanos/NANOS_FACTOR; //milliseconds
	}
	
}












